package com.dios.mirage.content;

import java.io.InputStream;

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.dios.mirage.component.Accordion;
import com.dios.mirage.component.AccordionPanel;
import com.dios.mirage.navigation.Navigation;

public class DealerListContent extends Content {
	
	private AccordionPanel accordionPanel;

	public void createGUI() {
		accordionPanel = new AccordionPanel();
		getData();
		add(accordionPanel);
	}

	public void getData() {
		InputStream is;
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder docBuilder;
		Document doc;

		try {
			is = this.getClass().getResourceAsStream("/xml/dealer.xml");
			docBuilder = docBuilderFactory.newDocumentBuilder();
			docBuilder.isValidating();
			doc = docBuilder.parse(is);
			Element element = doc.getDocumentElement();
			NodeList province = element.getElementsByTagName("province");
			for(int i=0;i<province.getLength();i++){
				Element provinceElement = (Element) province.item(i);
				Node provinceName = provinceElement.getElementsByTagName("name").item(0);
				Element outletsList = (Element) provinceElement.getElementsByTagName("outlets").item(0);
				NodeList outletList = outletsList.getElementsByTagName("outlet");
				VerticalFieldManager fieldManager = new VerticalFieldManager();
				for(int j=0;j<outletList.getLength();j++){
					Element outletElement = (Element) outletList.item(j);
					Node outletName = outletElement.getElementsByTagName("name").item(0);
					Node address = outletElement.getElementsByTagName("address").item(0);
					DealerList dealerList = new DealerList();
					dealerList.dealerNameField.setText(outletName.getFirstChild().getNodeValue());
					dealerList.addressField.setText(address.getFirstChild().getNodeValue());
					fieldManager.add(dealerList);
				}
				Accordion accordion = new Accordion(provinceName.getFirstChild().getNodeValue(), fieldManager);
				accordionPanel.add(accordion);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	private class DealerList extends VerticalFieldManager{
		public LabelField dealerNameField;
		public LabelField addressField;
		
		public DealerList(){
			dealerNameField = new LabelField(){

				protected void paint(Graphics graphics) {
					graphics.setColor(Color.RED);
					super.paint(graphics);
				}
				
			};
			dealerNameField.setFont(Navigation.loadCustomFonts(getClass().getResourceAsStream(Navigation.fontPath), Font.BOLD, 30));
			addressField = new LabelField(){

				protected void paint(Graphics graphics) {
					graphics.setColor(Color.WHITE);
					super.paint(graphics);
				}
				
			};
			addressField.setFont(Navigation.loadCustomFonts(getClass().getResourceAsStream(Navigation.fontPath), Font.ITALIC, 30));
			add(dealerNameField);
			add(addressField);
		}
	}

	/*private void updateField(String _node, String _element) {
		accordionPanel.
	}*/
}
